package win.zxshuo.www.spider;

import org.apache.http.HttpConnection;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@Component
public class BaseSpider {
    @Value("${spider.userAgent}")
    private String useragent;

    private Log logger = LogFactory.getLog(getClass());

    /**
      * @Description: 通过JSOUP 获取网页内容
      * @Author: RffanLAB
      * @Params: 
      * @Returns: 
      * @CreateDate: 2018/3/16
      */
    public Document getPageContent(String url) throws Exception{
        String ua = "";
        if (useragent == null){
            ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36";
        }else {
            ua = useragent;
        }
        Document document = new Document("");
        try {

            document = Jsoup.connect(url).userAgent(ua).get();

        }catch (Exception e){

            logger.error(e.getCause());
        }
        return document;
    }

    /**
      * @Description: 给Url编码
      * @Author: RffanLAB
      * @Params:
      * @Returns:
      * @CreateDate: 2018/3/16
      */
    public String encode(String toEncodeUrl){
        try{
            String encodedUrl = URLEncoder.encode(toEncodeUrl,"UTF-8");
            return encodedUrl;
        }catch (Exception e){
            logger.error(e.getMessage());
            return "";
        }
    }

    /**
      * @Description: URL解码
      * @Author: RffanLAB
      * @Params:
      * @Returns:
      * @CreateDate: 2018/3/16
      */
    public static String decode(String url){
        try {
            String prevURL="";
            String decodeURL=url;
            while(!prevURL.equals(decodeURL))
            {
                prevURL=decodeURL;
                decodeURL= URLDecoder.decode( decodeURL, "UTF-8" );
            }
            return decodeURL;
        } catch (UnsupportedEncodingException e) {
            return "Issue while decoding" +e.getMessage();
        }
    }



}
